package projects.jnurse.ioc.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by Adil Naji.
 * User: XP-170708
 * Date: 18 nov. 2008
 * Time: 17:17:25
 */
public class DynamicProxyInvocationHandler implements InvocationHandler {

    Object taget;
    AOPInterceptor interceptor;

    public DynamicProxyInvocationHandler(Object target, AOPInterceptor interceptor) {
        this.taget = target;
        this.interceptor = interceptor;
        
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try {
            interceptor.before(method, args);
            Field[] declaredFields = proxy.getClass().getDeclaredFields();
            List<Field> filtredFields = AnnotationFilter.getInstance().filter(declaredFields);

            Object returnValue = method.invoke(taget, args);
            interceptor.after(method, args);
            return returnValue;
        } catch (Throwable t) {
            interceptor.afterThrowing(method, args, t);
            throw t;
        } finally {
            interceptor.afterFinally(method, args);
        }
    }


}
